package com.sc.nft.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import com.sc.nft.enums.MallOrderRefundStatusEnum;
import com.sc.nft.enums.MallOrderRefundTypeEnum;
import com.sc.nft.enums.MallRefundStatusEnum;
import com.sc.nft.enums.RefundReasonEnum;
import com.sc.nft.sup.SuperEntity;
import com.sc.nft.util.RefundNoUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

/**
 * 订单售后表
 *
 * @author liGen
 * @version 1.0
 * @date 2023/11/17 3:47 下午
 */
@Data
@NoArgsConstructor
@Accessors(chain = true)
@TableName("sc_mall_order_refund")
public class MallOrderRefund extends SuperEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 订单ID
     */
    private Long orderId;

    /**
     * 用户Id
     */
    private Long userId;

    /**
     * 小程序用户Id
     */
    private Long miniUserId;
    /**
     * 售后类型
     */
    private MallOrderRefundTypeEnum refundType;

    /**
     * 订单商品ID
     */
    private Long orderGoodsId;

    /**
     * 订单编号
     */
    private String orderNo;

    /**
     * 售后编号
     */
    private String refundNo;

    /**
     * 订单支付时间
     */
    private Date orderPayTime;

    /**
     * 售后数量
     */
    private Integer refundNum;

    /**
     * 售后退款金额
     */
    private BigDecimal refundAmount;

    /**
     * 余额支付金额
     */
    private BigDecimal refundPayWalletPrice;
    /**
     * 红包支付金额
     */
    private BigDecimal refundPayBalancePrice;
    /**
     * 心愿礼金支付
     */
    private BigDecimal refundWishBalancePrice;

    /**
     * 订单金额
     */
    private BigDecimal orderAmount;

    /**
     * 订单实际支付金额
     */
    private BigDecimal orderPayAmount;

    /**
     * 申请原因
     */
    private String reason;

    /**
     * 申请描述
     */
    private String description;

    /**
     * 售后状态：
     */
    private MallRefundStatusEnum refundStatus;

    /**
     * 处理时间
     */
    private Date handleTime;

    /**
     * 凭证图片，以逗号隔开
     */
    private String proofPics;

    /**
     * 联系号码
     */
    private String contactNumber;

    /**
     * 商家售后快递单号
     */
    private String deliveryNo;

    /**
     * 商家售后快递名称
     */
    private String deliveryName;

    /**
     * 用户快递单号
     */
    private String userDeliveryNo;

    /**
     * 用户快递名称
     */
    private String userDeliveryName;

    /**
     * 备注
     */
    private String remark;

    /**
     * 订单售后表参数封装
     *
     * @param mallOrder        商城订单
     * @param mallOrderProduct 商城订单商品
     * @param refundType       售后类型
     * @param refundNum        售后数量
     * @param reason           申请原因
     * @param proofPics        凭证图片，以逗号隔开
     * @param contactNumber    联系号码
     * @return
     */
    public static MallOrderRefund build(MallOrder mallOrder, MallOrderProduct mallOrderProduct, MallOrderRefundTypeEnum refundType,
                                        Integer refundNum, RefundReasonEnum reason, String description, String proofPics, String contactNumber, BigDecimal refundPrice) {
        MallOrderRefund mallOrderRefund = new MallOrderRefund();
        mallOrderRefund.setOrderId(mallOrder.getId());
        mallOrderRefund.setUserId(mallOrder.getUserId());
        mallOrderRefund.setMiniUserId(mallOrder.getMiniUserId());
        mallOrderRefund.setRefundType(refundType);
        mallOrderRefund.setOrderGoodsId(mallOrderProduct.getId());
        mallOrderRefund.setOrderNo(mallOrder.getOrderNo());
        mallOrderRefund.setRefundNo(RefundNoUtil.getRefundNo());
        mallOrderRefund.setOrderPayTime(mallOrder.getPayTime());
        mallOrderRefund.setRefundNum(refundNum);
        // 售后退款金额
        mallOrderRefund.setRefundAmount(refundPrice);
        mallOrderRefund.setOrderAmount(mallOrder.getPayPrice());
        mallOrderRefund.setOrderPayAmount(mallOrder.getRealPrice());
        mallOrderRefund.setReason(reason.getDescription());
        mallOrderRefund.setDescription(description);
        // 申请中
        mallOrderRefund.setRefundStatus(MallRefundStatusEnum.APPLYING);
        mallOrderRefund.setProofPics(proofPics);
        mallOrderRefund.setContactNumber(contactNumber);
        return mallOrderRefund;
    }


    /**
     * 数据处理
     *
     * @param mallOrder 商城订单
     * @return
     */
    public static MallOrderRefund buildByOldOrder(MallOrder mallOrder, MallOrderProduct mallOrderProduct, MallRefundStatusEnum mallRefundStatusEnum) {
        MallOrderRefund mallOrderRefund = new MallOrderRefund();
        mallOrderRefund.setOrderId(mallOrder.getId());
        mallOrderRefund.setUserId(mallOrder.getUserId());
        mallOrderRefund.setRefundType(MallOrderRefundTypeEnum.ONLY_REFUND);
        mallOrderRefund.setOrderGoodsId(mallOrderProduct.getId());
        mallOrderRefund.setOrderNo(mallOrder.getOrderNo());
        mallOrderRefund.setRefundNo(RefundNoUtil.getRefundNo());
        mallOrderRefund.setOrderPayTime(new Date());
        mallOrderRefund.setRefundNum(mallOrderProduct.getRefundNum());
        // 售后退款金额
        mallOrderRefund.setRefundAmount(mallOrderProduct.getRefundPrice());
        mallOrderRefund.setOrderAmount(mallOrder.getPayPrice());
        mallOrderRefund.setOrderPayAmount(mallOrder.getRealPrice());
        mallOrderRefund.setReason(mallOrder.getRefundReasonWap());
        mallOrderRefund.setDescription("");
        mallOrderRefund.setRefundStatus(mallRefundStatusEnum);
        mallOrderRefund.setProofPics(mallOrder.getRefundReasonWapImg());
        mallOrderRefund.setContactNumber(mallOrder.getUserTel());
        return mallOrderRefund;
    }
}
